package me.numark.view;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.CardView;
import android.text.Html;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.joooonho.SelectableRoundedImageView;
import com.makeramen.roundedimageview.RoundedTransformationBuilder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.utils.Utility;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.Inflater;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.numark.MainActivity;
import me.numark.R;
import me.numark.activities.ArticleActivity;
import me.numark.activities.ArticleDetailActivity;
import me.numark.activities.BaseActivity;
import me.numark.activities.MarkDetailActivity;
import me.numark.activities.ProfileActivity;
import me.numark.activities.ReportActivity;
import me.numark.activities.TagActivity;
import me.numark.fragments.AskLoginFragment;
import me.numark.fragments.BaseFragment;
import me.numark.fragments.HotFragment;
import me.numark.model.DailyData;
import me.numark.model.Feed;
import me.numark.model.FeedResponse;
import me.numark.model.FeedTag;
import me.numark.model.User;
import me.numark.net.BaseResponse;
import me.numark.net.NetworkConstants;
import me.numark.net.RequestServerTask;
import me.numark.utils.Constants;
import me.numark.utils.Global;
import me.numark.utils.HttpUtil;
import me.numark.utils.Settings;
import me.numark.utils.ShareContentWebpage;
import me.numark.utils.SinaShareManager;
import me.numark.utils.Utils;
import me.numark.view.checktagview.TagLayoutView;
import me.numark.view.checktagview.TagListView;


/**
 * Created by Evan on 15/7/24.
 */
public class FeedViewHolder extends BaseViewHolder implements View.OnClickListener, IUiListener, TagListView.OnTagCheckedChangedListener {

    @Bind(R.id.header_comment_number)
    TextView headerMarkNum;
    @Bind(R.id.feed_item_label_num)
    TextView feedItemLabelNum;

    @Bind(R.id.avatar)
    public CircleImageView avatar;

    @Bind(R.id.name)
    public TextView name;

    @Bind(R.id.date)
    public TextView date;

    @Bind(R.id.pic)
    public SelectableRoundedImageView pic;

    @Bind(R.id.news_title)
    public TextView newsTitle;

    @Bind(R.id.feed_view)
    FrameLayout feedView;


    @Bind(R.id.news_view)
    public View newsView;

    @Bind(R.id.header_comment_layout)
    LinearLayout commentLayout;

    @Bind(R.id.avatar_layout)
    RelativeLayout avatarLayout;

    @Bind(R.id.tagview)
    TagListView mTagListView;

    @Bind(R.id.more)
    ImageView rightImage;

    @Bind(R.id.mongolia_layer)
    FrameLayout mongoliaLayer;

    Context mContext;
    int mFeedType;
    public int mOrigFeedType;

    View refreshingView;
    View expandView;
    boolean isMarkDetail = false;
    PopupMoreView mPopupMoreView;
    public static final String FEED_ID = "feed_id";
    private String title;

    public FeedViewHolder(View itemView,Object parent,int feedtype, boolean isMarkDetail,boolean needHiddenavatarLayout){
        this(itemView,parent,feedtype,needHiddenavatarLayout);
        this.isMarkDetail = isMarkDetail;

    }
    boolean needHidden;

    public FeedViewHolder(View itemView, Object parent, int feedtype,boolean needHiddenavatarLayout) {
        super(itemView, parent);
        mFeedType = feedtype;
        needHidden = needHiddenavatarLayout;
        //newsCardView.setPreventCornerOverlap(false);
        if (feedtype == Constants.FEEDTYPE_LOADMORE) {
            refreshingView = itemView.findViewById(R.id.loading);
            expandView = itemView.findViewById(R.id.expend_icon);
            addOnClickListener(itemView);
            return;
        }
        if (feedtype == Constants.FEEDTYPE_NONE)
            return;
        if (mActivity != null) {
            mContext = mActivity;
        } else if (mFragment != null) {
            mContext = mFragment.getActivity();
        } else {
            mContext = itemView.getContext();
        }
        ButterKnife.bind(this, itemView);
        name.setMovementMethod(LinkMovementMethod.getInstance());
        addOnClickListener(avatar);
        addOnClickListener(newsView);
        addOnClickListener(rightImage);
        addOnClickListener(feedView);
        if (mFeedType == Constants.FEEDTYPE_TAG_HOT){
            avatarLayout.setVisibility(View.GONE);
        }
        if (mFeedType == Constants.FEEDTYPE_DETAIL) {
            itemView.setBackgroundResource(R.drawable.detail_header_bg);
            commentLayout.setVisibility(View.VISIBLE);
            mTagListView.setVisibility(View.GONE);
            rightImage.setVisibility(View.GONE);
            feedItemLabelNum.setVisibility(View.GONE);
            if(needHiddenavatarLayout){
                avatarLayout.setVisibility(View.GONE);
            }else {
                avatarLayout.setVisibility(View.VISIBLE);
            }
        }
        if (mFeedType == Constants.FEEDTYPE_SEARCH_ALL){
            avatarLayout.setVisibility(View.GONE);
        }
    }



    private Feed mFeed;
    private DailyData mDay;

    public void bindData(DailyData day) {
        if (day == null) {
            return;
        }
        mDay = day;
        mFeed = null;
        if (day.isRefreshing) {
            refreshingView.setVisibility(View.VISIBLE);
            expandView.setVisibility(View.GONE);
        } else {
            refreshingView.setVisibility(View.GONE);
            expandView.setVisibility(View.VISIBLE);
        }
    }


    Object adapter;

    public void bindData(Feed feed,Object adapter,int position) {

        this.adapter = adapter;
        if (!isMarkDetail){

        }
        if (feed == null) {
            return;
        }
        mDay = null;
        //////////////////////////
       // feed.news.labels = mTags;
        ////////////////////////
        feed.position = position;
        mFeed = feed;

        User user = getMainUser();

        if (user!=null) {
            String picUrl = user.pic;
            avatar.setBorderColor(Global.avatarColor);
            avatar.setBorderWidth(2);
            if (!TextUtils.isEmpty(picUrl))
                Picasso.with(mContext).load(picUrl).into(avatar);
            else
                Picasso.with(mContext).load(R.drawable.ic_launcher).into(avatar);

            long time = feed.create_timestamp;
            long topicTimestamp = time * 1000L;
            long topicJetLag = Global.getCurrentTime() - topicTimestamp;
            if (topicJetLag < Utils.ONE_MINUTE) {
                date.setText("刚刚");
            } else if (topicJetLag < Utils.ONE_HOUR) {
                date.setText(topicJetLag / (1000 * 60) + " 分钟前");
            } else {
                date.setText(Utils.getDatetimeStr(time * 1000L));
            }

            CharSequence nameText = getNameText(feed);
            name.setText(nameText, TextView.BufferType.SPANNABLE);
        }
            if (TextUtils.isEmpty(feed.news.face_img)) {

                pic.setVisibility(View.GONE);
            } else {
                pic.setVisibility(View.VISIBLE);
                Picasso.with(mContext).load(feed.news.face_img).into(pic);
            }

            newsTitle.setText(feed.news.title);




        if (feed.labels!=null) {
            mTagListView.setTags(feed.labels, true);
            mTagListView.setDeleteMode(false);
            mTagListView.setOnTagCheckedChangedListener(this);
        }

        if (mFeedType == Constants.FEEDTYPE_TAG_HOT){
            headerMarkNum.setText(String.valueOf(feed.news.label_num));
            feedItemLabelNum.setText(String.valueOf(feed.news.label_num));
        }else if (mFeedType == Constants.FEEDTYPE_TIMELINE){
            headerMarkNum.setText(String.valueOf(feed.label_num));
            feedItemLabelNum.setText(String.valueOf(feed.label_num));
        }else if (mFeedType == Constants.FEEDTYPE_DETAIL) {
            if(needHidden){
                headerMarkNum.setText(String.valueOf(feed.news.label_num));
                feedItemLabelNum.setText(String.valueOf(feed.news.label_num));
            }else {
                headerMarkNum.setText(String.valueOf(feed.label_num));
                feedItemLabelNum.setText(String.valueOf(feed.label_num));
            }
        }

    }

    @Override
    public void onTagCheckedChanged(final TagLayoutView tagLayoutView, final FeedTag tag) {
        //点赞+1或点赞-1请求
        if (!Global.isLoggedIn()){
            AskLoginFragment.show((BaseActivity) mContext);
            tagLayoutView.tagView.setChecked(tag.self_like);
            return;
        }
        /**
         * 如果是自己发起的标签 点击标签删除
         */
        if (tag.user.uid == Global.getCurrentUid()){
            tagLayoutView.tagView.setChecked(tag.self_like);
            showDialog(tag);
        }else {
            /**
             * 如果是点过赞的标签 再次点击取消点赞
             */
            new RequestServerTask<FeedResponse>(FeedResponse.class) {
                @Override
                protected String requestServer() {
                    String url = String.format(tag.self_like == true ? NetworkConstants.LABEL_HATE : NetworkConstants.LABEL_LIKE,
                            Global.getCurrentUid());
                    Map<String, Object> map = new HashMap();
                    map.put("label_id", tag.id);
                    if (mFeedType == Constants.FEEDTYPE_TAG_HOT) {
                        map.put("timeline_id", 0);
                    } else {
                        map.put("timeline_id", mFeed.id);
                    }
                    return HttpUtil.post(url, map);
                }
                @Override
                protected boolean onFailure(FeedResponse result) {
                    tagLayoutView.tagView.setChecked(tag.self_like);
                    return super.onFailure(result);

                }
                @Override
                protected void onSuccess(FeedResponse result) {
                    tag.self_like = !tag.self_like;
                    if (tag.self_like){
                        mongoliaLayer.setVisibility(View.VISIBLE);
                        Animator animator = AnimatorInflater.loadAnimator(mContext, R.animator.update_msg_anim);
                        animator.setTarget(mongoliaLayer);
                        animator.start();
                        mHandler.sendEmptyMessageDelayed(95, 2000);
                        tag.like_num++;
                    }else {
                        tag.like_num--;
                    }

                    tagLayoutView.textView.setText(String.valueOf(tag.like_num));
                   // tagLayoutView.tagView.setChecked(tag.self_like);
                    if (tag.self_like){
                        tagLayoutView.textView.setTextColor(Color.parseColor("#ffffff"));
                    }else {
                        tagLayoutView.textView.setTextColor(Color.parseColor("#00b16a"));
                    }
                }

            }.start();
        }
    }


    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(android.os.Message msg)
        {
            if(msg.what == 95)
            {
                mongoliaLayer.setVisibility(View.GONE);
            }
        }
    };


    private  class MyURLSpan extends ClickableSpan {
        private String mUrl;
        MyURLSpan(String url) {
            mUrl =url;
        }
        @Override
        public void onClick(View widget) {
            int atUid = -100;
            //TODO 版本修改需要更改
            Pattern p = Pattern.compile("http://api.noomark.com/v1_1/account/(.+?)/index");
            Matcher m = p.matcher(mUrl);
            while (m.find()){
               atUid = Integer.parseInt(m.group(1));
            }
            if (atUid != -100){
                ProfileActivity.openProfile(mContext, atUid);
            }

        }
    }

    CharSequence getNameText(Feed feed) {

                SpannableString nameStr = null;
                nameStr = feed.user.getNameInFeed();
                nameStr.setSpan(new ForegroundColorSpan(Color.rgb(153,153,153)), 0, nameStr.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                //#999999String actionText = null;
               /* if (feed.news.star >= Constants.STAR_LEVEL) {
                    actionText = mContext.getString(R.string.marked_an_article);
                } else {
                    actionText = mContext.getString(R.string.marked_an_article2);
                }
                SpannableString actionStr = new SpannableString(actionText);
                actionStr.setSpan(new ForegroundColorSpan(Color.rgb(153,153,153)), 0, actionStr.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);*/
                return nameStr;
                //TextUtils.concat(nameStr, actionStr);
    }

    User getMainUser() {
        switch (mFeedType) {

            case Constants.FEEDTYPE_USER_LIKES:
                return mFeed.user;
            case Constants.FEEDTYPE_DETAIL:
                return mFeed.user;
            default:
                return mFeed.user;
        }
    }

    public String getNewsUrl(){
        String urlString = null;
        if (mFeed.id==0){
            urlString = String.format(NetworkConstants.SHARE_URL,mFeed.news.id,1);
        }else {
            urlString = String.format(NetworkConstants.SHARE_URL,mFeed.id,0);
        }
        return urlString;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more:
                showMore();
                break;
            case R.id.news_view: {
                Intent intent = new Intent(mContext, ArticleActivity.class);
                intent.putExtra(Constants.NEWS_EXTRA, mFeed.news.toString());
                intent.putExtra(Constants.FEED_ID,mFeed.id);
                intent.putExtra(Constants.SHARE_COUNT, mFeed.news.mark_num);
                mContext.startActivity(intent);
            }
            break;
            case R.id.feed_view: {
                if (mFeed != null) {
                    Intent intent = new Intent(mContext, MarkDetailActivity.class);
                    intent.putExtra(Constants.FEED_EXTRA, mFeed.toString());
                    intent.putExtra(MarkDetailActivity.EXTRA_ORIG_TYPE, mFeedType);
                    mContext.startActivity(intent);
                }
            }
            break;
            case R.id.load_more: {
                if (mDay != null) {
                    if (!mDay.isRefreshing) {
                        mDay.isRefreshing = true;
                        bindData(mDay);
                        HotFragment hotFragment = (HotFragment) mFragment;
                        hotFragment.loadData(mDay.date, mDay.data.size());
                    }
                }
            }
            break;
            case R.id.avatar:
                ProfileActivity.openProfile(mContext, getMainUser().uid);
                break;
            case R.id.more_browser: {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mFeed.news.url));
                mContext.startActivity(browserIntent);
                dismissShowMore();
            }
            break;
            case R.id.more_copy: {
                ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                clipboard.setPrimaryClip(ClipData.newPlainText("url", mFeed.news.url));
                Utils.showToast(mContext.getString(R.string.already_copy_url));
                dismissShowMore();
            }
            break;
            case R.id.more_delete: {
                dismissShowMore();
                new RequestServerTask<BaseResponse>(BaseResponse.class, mContext, mContext.getString(R.string.deleting)) {
                    @Override
                    protected String requestServer() {
                        String url = String.format(NetworkConstants.FEED_DELETE_URL, Global.getCurrentUid(), mFeed.id);
                        return HttpUtil.get(url, null);
                    }

                    @Override
                    protected void onSuccess(BaseResponse result) {
                        Intent intent = new Intent(Constants.ACTION_FEED_DELETED);
                        intent.putExtra(Constants.FEED_MID_EXTRA, mFeed.id);
                        LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
                        if (isMarkDetail){
                            mActivity.finish();
                        }
                    }
                }.start();
            }
            break;
            case R.id.share_weixin:
                dismissShowMore();
                Log.d("FeedViewHolder",getNewsUrl());
                Utils.wechatShare(0, getNewsUrl(), mFeed.news.title, title, getNewsPic());
                break;
            case R.id.share_circle:
                dismissShowMore();
                Utils.wechatShare(1, getNewsUrl(), mFeed.news.title, title, getNewsPic());
                break;
            case R.id.share_qq: {
                dismissShowMore();
                Tencent mTencent = null;
                try {
                    mTencent = Tencent.createInstance(Constants.TencentAppId, mContext);
                } catch (Throwable e) {
                    e.printStackTrace();
                    return;
                }
                final Bundle params = new Bundle();
                params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
                params.putString(QQShare.SHARE_TO_QQ_TITLE, mFeed.news.title);
                params.putString(QQShare.SHARE_TO_QQ_SUMMARY,title);
                params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, getNewsUrl());
                params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, getNewsPicUrl());
                params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "怒马");
//                params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,  "其他附加功能");
                mTencent.shareToQQ(mActivity == null ? mFragment.getActivity() : mActivity, params, this);
            }
            break;
            case R.id.share_qzone: {
                dismissShowMore();
                Tencent mTencent = null;
                try {
                    mTencent = Tencent.createInstance(Constants.TencentAppId, mContext);
                } catch (Throwable e) {
                    e.printStackTrace();
                    return;
                }
                final Bundle params = new Bundle();
                params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
                params.putString(QzoneShare.SHARE_TO_QQ_TITLE, mFeed.news.title);
                params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY,  title);
                ArrayList<String> value = new ArrayList<>();
                value.add(getNewsPicUrl());
                params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, value);
                params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, getNewsUrl());
               // params.putString(QzoneShare.SHARE_TO_QQ_IMAGE_URL,mFeed.news.face_img);
                params.putString(QzoneShare.SHARE_TO_QQ_APP_NAME, "怒马");
//                params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,  "其他附加功能");
                mTencent.shareToQzone(mActivity == null ? mFragment.getActivity() : mActivity, params, this);
            }
            break;
            case R.id.share_weibo: {
                dismissShowMore();
                SinaShareManager manager = new SinaShareManager();
                manager.registSina(mContext);
                ShareContentWebpage webpage
                        = new ShareContentWebpage(mFeed.news.title,"",getNewsUrl(),getNewsPic());
                manager.shareWebPage(webpage,mContext);
            }
            break;
            case R.id.more_report:
                dismissShowMore();
                Intent intent = new Intent(mContext, ReportActivity.class);
                intent.putExtra(FEED_ID, mFeed.id);
                mContext.startActivity(intent);
                break;

            case R.id.no_display_button:
                doNoDisplay();
                mPopupMoreView.dismiss();
                break;
            case R.id.no_watch_button:
                doUnFollow();
                mPopupMoreView.dismiss();
                break;
        }
    }
    void doNoDisplay(){
        int msg = R.string.submit_nodisplay;
        new RequestServerTask<BaseResponse>(BaseResponse.class, this, mContext.getString(msg)) {
            int userId;
            long mid;
            @Override
            protected String requestServer() {
                String baseUrl = NetworkConstants.TIME_LINE_READ;

                if (mFeed.user != null){
                    userId = mFeed.user.uid;
                }
                mid = mFeed.id;
                String url = String.format(baseUrl, Global.getCurrentUid(),mid);
                return HttpUtil.get(url, null);
            }

            @Override
            protected void onSuccess(BaseResponse result) {
                Intent intent = new Intent(Constants.ACTION_FEED_DELETED);
                intent.putExtra(Constants.FEED_MID_EXTRA, mFeed.id);
                LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
            }
        }.start();
    }
    void doUnFollow() {
        int msg = R.string.submit_unfollow;
        new RequestServerTask<BaseResponse>(BaseResponse.class, this, mContext.getString(msg)) {
            int userId;
            @Override
            protected String requestServer() {
                String baseUrl = NetworkConstants.UNFOLLOW_URL;

                if (mFeed.user != null){
                    userId = mFeed.user.uid;
                }
                String url = String.format(baseUrl, Global.getCurrentUid(), userId);
                return HttpUtil.get(url, null);
            }

            @Override
            protected void onSuccess(BaseResponse result) {
                if (adapter!=null){
                    if (adapter instanceof FeedSimpleAdapter) {
                        FeedSimpleAdapter adapter = (FeedSimpleAdapter) FeedViewHolder.this.adapter;
                        adapter.removeFromUser();
                    }
                    if (adapter instanceof HotFragment.HotAdapter){
                        HotFragment.HotAdapter adapter = (HotFragment.HotAdapter)FeedViewHolder.this.adapter;
                        adapter.removeFromUser();
                    }
                }
            }
        }.start();
    }

    public Bitmap getNewsPic() {
        ImageView imageView = new ImageView(mContext);
        if (TextUtils.isEmpty(mFeed.news.face_img)) {
            Picasso.with(mContext).load(R.drawable.share_default).into(imageView);
        } else {
            Picasso.with(mContext).load(mFeed.news.face_img)
                    .into(imageView);
        }
        Drawable drawable = imageView.getDrawable();

        if (drawable != null){
            return Utils.drawableToBitmap(drawable);
        }
        return null;
    }

    public String getNewsPicUrl(){
        if (TextUtils.isEmpty(mFeed.news.face_img)){
           return NetworkConstants.NEWS_DEFAULT_URL;
        }else
            return mFeed.news.face_img;
    }

    public void dismissShowMore() {
        if (mPopupMoreView != null) {
            mPopupMoreView.dismiss();
            mPopupMoreView = null;
        }
    }

    public void showMore() {
        dismissShowMore();
        if (mFeedType != Constants.FEEDTYPE_DETAIL) {
            mOrigFeedType = mFeedType;
        }
        mPopupMoreView = new PopupMoreView(mContext);

        if (mOrigFeedType != Constants.FEEDTYPE_TIMELINE
                && mOrigFeedType != Constants.FEEDTYPE_HOT_MARK
                && mOrigFeedType != Constants.FEEDTYPE_LAST_MARK) {
            mPopupMoreView.noDisplayButton.setVisibility(View.GONE);
            mPopupMoreView.noWatchButton.setVisibility(View.GONE);
        }
        //如果是我自己发的mark 没有举报与不再显示 不再关注
        if (mFeedType==Constants.FEEDTYPE_TIMELINE||mFeedType==Constants.FEEDTYPE_DETAIL) {
            if (mFeed.id!=0) {
                if (mFeed.user.uid == Global.getCurrentUid()) {
                    mPopupMoreView.moreReport.setVisibility(View.GONE);
                    mPopupMoreView.noDisplayButton.setVisibility(View.GONE);
                    mPopupMoreView.noWatchButton.setVisibility(View.GONE);
                } else {
                    mPopupMoreView.moreDelete.setVisibility(View.GONE);
                }
            }else {
                mPopupMoreView.moreReport.setVisibility(View.GONE);
                mPopupMoreView.noDisplayButton.setVisibility(View.GONE);
                mPopupMoreView.noWatchButton.setVisibility(View.GONE);
                mPopupMoreView.moreDelete.setVisibility(View.GONE);
            }
        }
        if (mFeedType == Constants.FEEDTYPE_USER_MARKS){
            if (mFeed.id!=0) {
                mPopupMoreView.noDisplayButton.setVisibility(View.GONE);
                mPopupMoreView.noWatchButton.setVisibility(View.GONE);
                if (mFeed.user.uid == Global.getCurrentUid()) {
                    mPopupMoreView.moreReport.setVisibility(View.GONE);
                    mPopupMoreView.moreDelete.setVisibility(View.VISIBLE);
                }else {
                    mPopupMoreView.moreReport.setVisibility(View.VISIBLE);
                    mPopupMoreView.moreDelete.setVisibility(View.GONE);
                }
            }
        }
       /* //如果是我转发的 没有举报与不再显示 不再关注
        if (mFeed.forward!=null && mFeed.forward.user.uid == Global.getCurrentUid()){
            mPopupMoreView.moreReport.setVisibility(View.GONE);
            mPopupMoreView.noDisplayButton.setVisibility(View.GONE);
            mPopupMoreView.noWatchButton.setVisibility(View.GONE);
        }
        //如果是别人转发的或者是别人发布的mark 没有删除
        if (mFeedType==Constants.FEEDTYPE_TIMELINE) {
            if ((mFeed.forward != null && mFeed.forward.user.uid != Global.getCurrentUid()) || (mFeed.user.uid != Global.getCurrentUid() && mFeed.forward == null)) {
                mPopupMoreView.moreDelete.setVisibility(View.GONE);
            }
        }*/
        /*if (mFeed.user.is_follow){
            mPopupMoreView.noWatchButton.setVisibility(View.VISIBLE);
        }else{
            mPopupMoreView.noWatchButton.setVisibility(View.GONE);
        }*/
        //如果当前用户没登录 没有举报删除 不再显示与不再关注
        if (!Global.isLoggedIn()) {
            mPopupMoreView.noDisplayButton.setVisibility(View.GONE);
            mPopupMoreView.noWatchButton.setVisibility(View.GONE);
            mPopupMoreView.moreReport.setVisibility(View.GONE);
            mPopupMoreView.moreDelete.setVisibility(View.GONE);
        }
        //如果是黑衣人发布的且不是转发的 没有不再关注，没有删除
       /* if (mFeedType==Constants.FEEDTYPE_TIMELINE) {
            if (mFeed.user.uid == -1 && mFeed.forward == null) {
                mPopupMoreView.noWatchButton.setVisibility(View.GONE);
                mPopupMoreView.moreDelete.setVisibility(View.GONE);
            }

            //如果是怒小哥发布的且不是转发的 没有不再显示 不再关注 举报 删除
            if (mFeed.user.uid == 1 && mFeed.forward == null) {
                mPopupMoreView.noWatchButton.setVisibility(View.GONE);
                mPopupMoreView.noDisplayButton.setVisibility(View.GONE);
                mPopupMoreView.moreReport.setVisibility(View.GONE);
                mPopupMoreView.moreDelete.setVisibility(View.GONE);
            }
        }*/
        //热推内容 没有不再显示与不再关注
        if (adapter instanceof HotFragment.HotAdapter){
            mPopupMoreView.moreDelete.setVisibility(View.GONE);
            mPopupMoreView.noWatchButton.setVisibility(View.GONE);
            mPopupMoreView.noDisplayButton.setVisibility(View.GONE);
        }

        mPopupMoreView.noDisplayButton.setOnClickListener(this);
        mPopupMoreView.noWatchButton.setOnClickListener(this);
        mPopupMoreView.moreBrowser.setOnClickListener(this);
        mPopupMoreView.moreCopy.setOnClickListener(this);
        mPopupMoreView.moreDelete.setOnClickListener(this);
        mPopupMoreView.moreReport.setOnClickListener(this);
        mPopupMoreView.shareCircle.setOnClickListener(this);
        mPopupMoreView.shareQQ.setOnClickListener(this);
        mPopupMoreView.shareQZone.setOnClickListener(this);
        mPopupMoreView.shareWeibo.setOnClickListener(this);
        mPopupMoreView.shareWeixin.setOnClickListener(this);
        mPopupMoreView.show();
    }

    @Override
    public void onComplete(Object o) {

    }

    @Override
    public void onError(UiError uiError) {

    }

    @Override
    public void onCancel() {

    }

    void showDialog(final FeedTag tag) {
        View view = ((MainActivity)mContext).getLayoutInflater().inflate(R.layout.delete_topic, null);
        Button deleteBtn = (Button) view.findViewById(R.id.btn_delete);
        Button cancelBtn = (Button)view.findViewById(R.id.btn_cancel);
        final Dialog dialog = new Dialog(mContext, R.style.transparentFrameWindowStyle);
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                new RequestServerTask<FeedResponse>(FeedResponse.class) {
                    @Override
                    protected String requestServer() {
                        String url = String.format(NetworkConstants.LABEL_DELETE, Global.getCurrentUid());
                        Map<String, Object> map = new HashMap();
                        map.put("label_id", tag.id);
                        return HttpUtil.post(url, map);
                    }

                    @Override
                    protected void onSuccess(FeedResponse result) {
                        for(int i=0;i<mFeed.labels.size();i++){
                            FeedTag f = mFeed.labels.get(i);
                            if (f.id == tag.id){
                                mFeed.labels.remove(i);
                                break;
                            }
                        }
                        mTagListView.removeTag(tag);
                        mFeed.news.label_num--;
                        mFeed.deleteFeedTag = tag;
                        bindData(mFeed,adapter,position);
                        notifyToRefresh();
                    }
                }.start();

            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        Window window = dialog.getWindow();

        window.setWindowAnimations(R.style.menu_animstyle);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = ((MainActivity)mContext).getWindowManager().getDefaultDisplay().getHeight();

        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        dialog.onWindowAttributesChanged(wl);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    private void notifyToRefresh(){
        Intent intent = new Intent();
        intent.setAction(Constants.NOTIFY);
        if (mFeed!=null) {
            Bundle bundle = new Bundle();
            bundle.putString(Constants.NOTIFY_ACTION, mFeed.toString());
            intent.putExtras(bundle);
            LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
        }
    }
}
